<?php

namespace Data;

class Comments extends Base {

	public function addComment($userId, $articleId, $content) {
		$data = array(
			'UserId' => $userId,
			'ArticleId' => $articleId,
			'Content' => $content,
			'CreateTime' => time(),
			'Status' => self::IS_YES,
		);
		$this->insert($data);
	}

	public function getCommentsByArticleId($articleId, $pager) {
		$condition = array(
			'c.ArticleId' => $articleId,
			'c.Status' => self::IS_YES,
			'u.Status' => self::IS_YES,
		);
		$result = $this->select('c.*, u.Email, u.Username, u.Sex, u.Photo')
			->from('Comments as c')
			->innerJoin('User as u', 'u.UserId = c.UserId')
			->where($condition)
			->order('CommentId desc')
			->pager($pager)
			->findAll();
		return $result;
	}

	public function getCountByIds($articleIds) {
		$condition = array(
			'ArticleId in' => $articleIds,
			'Status' => self::IS_YES,
		);
		$data = $this->select('count(1) as number, ArticleId')->where($condition)->group('ArticleId')->findAll();
		$result = array();
		foreach($data as $val) {
			$result[$val['ArticleId']] = $val['number'];
		}
		return $result;
	}
}